(function() {

    'use strict';

    angular
        .module('app')
        .controller('HomeController', HomeController);

    HomeController.$inject = ['$scope', '$http', '$rootScope', '$timeout', '$location', '$state', 'API', 'ENUM', 'CONSTANTS', '$window', 'AppAuthenticationService', 'CartModel', 'ConfigModel'];

    function HomeController($scope, $http, $rootScope, $timeout, $location, $state, API, ENUM, CONSTANTS, $window, AppAuthenticationService, CartModel, ConfigModel) {

        var MAX_BANNERS = 10;

        $scope.banners = [];

        $scope.touchSearch = function() {
            $state.go('search', {});
        };

        $scope.touchBanner = function(data){
            if (!data.redirect || !data.redirect.length) {
                $scope.toast('没有链接');
                return;
            }

            $window.location.href = data.redirect;
        };

        $scope.touchProduct = function(product) {
            $state.go('product', {
                product: product.id,
            });
        };

        $scope.touchGroup = function(group) {
            $state.go('home', {

            });
        };

        $scope.formatUrl = function(url) {
            var timestamp = Math.round(new Date().getTime() / 1000);

            if (-1 == url.indexOf('?')) {
                return url + '?v=' + timestamp;
            } else {
                return url + '&v=' + timestamp;
            }
        };
        
        $scope.cartModel = CartModel;

        function _reloadBanners() {
            API.banner.list({
                    page: 1,
                    per_page: MAX_BANNERS
                }).then(function(banners) {
                    $scope.banners = banners;
                    var timer = $timeout(function() {
                        $scope.bannerSwiper = new Swiper('.home-banner', {
                            pagination: '.swiper-pagination',
                            paginationClickable: true,
                            spaceBetween: 30,
                            centeredSlides: true,
                            autoplay: 1500,
                            autoplayDisableOnInteraction: false,
                            loop: true,
                        });
                    }, 1);
                });
        }

        $scope.refreshCode = function() {
            var timestamp = Math.round(new Date().getTime() / 1000);
        };

        function _reload() {
            _reloadBanners();

            ConfigModel.fetch().then(function() {
                var config = ConfigModel.getConfig();
                var wechat = config['wechat.web'];
                if (wechat && CONSTANTS.FOR_WEIXIN && !AppAuthenticationService.getOpenId()) {
                    if ($rootScope.isWeixin()) {console.log('===');
                        // $state.go('wechat-authbase', {});
                        $state.go('wechat-auth', {});
                        return;
                    }
                }
            });

            $scope.cartModel.reloadIfNeeded();
        }


        _reload();
    }

})();
